<?php

namespace app\frontend\controller;

use app\common\model\BgComments;
use app\common\model\BgPosts;
use app\common\model\Zans;
use Carbon\Carbon;
use think\Controller;

use think\Request;

class Post extends Controller
{

    //前置操作
    protected $beforeActionList=[
        'first',
    ];

    public function first(){
        if(!session('uid') || !session('name')) {
            return $this->redirect('/login');
        }


    }

    public function index()
    {
        //1524457676
        /*$time=new Carbon();
        $res=$time->timestamp=1524457676;*/
        /*$time=1524457676;
        $result=$time->toFormattedDateString();
        dump($result);*/
        $posts=BgPosts::where('status',1)->order('id','desc')->paginate(5);
        $this->assign(compact('posts'));
        return $this->fetch();
    }

    //文章创建
    public function create(Request $request){
//        dump($request->post());

        if($request->isGet()) {
            return $this->fetch();
        }

        //获取请求信息
        $title=$request->post('title');
        $content=$request->post('content');

        //验证信息
        $err_msg=$this->validate($request->post(),[
            'title'=>'require|max:30|unique:bg_posts|token',
            'content'=>'require|max:1000|unique:bg_posts'
        ],[
            'title.require'=>'请填写文章标题',
            'title.max'=>'文章标题不能大于30个字符',
            'titile.unique'=>'此文章标题已存在',
            'content.require'=>'请填写文章内容',
            'content.max'=>'文章内容不能大于1000个字符',
            'content.unique'=>'此文章内容已存在'
        ]);

        if($err_msg !==true){
            return renderJs($err_msg);
        }

        //写入数据库
        $post=new BgPosts;
        $post->title=$title;
        $post->content=$content;
        $post->uid=session('uid');
        $post->save();

        return renderJs('发布成功','/postss');
    }

    public function show(Request $request,$id){
        if($request->isGet()){
            $post_info=BgPosts::where('status',1)->where('id',$id)->find();
            //$post_info=BgPosts::get($id);
            if(!$post_info){
                return renderJs('非法操作','/postss');
            }

            $comments=BgComments::where('status',1)->where('post_id',$id)->select();

            //赞查询
            $zan=Zans::where('post_id',$id)->where('user_id',session('uid'))->find();

            $this->assign(compact('post_info','comments','zan'));

            return $this->fetch();
        }
    }

    public function edit(Request $request,$id){
        if($request->isGet()){
            $post_info=BgPosts::where('status',1)->where('id',$id)->find();
            if(!$post_info || session('uid') !== $post_info->uid || !session('uid')){
                return renderJs('非法操作','/postss');
            }
            $this->assign(compact('post_info'));
            return $this->fetch();
        }

        if($request->isPut()){
            //dump($request->put());
            //获取信息
            $title=$request->put('title');
            $content=$request->put('content');

            //验证
            $err_msg=$this->validate($request->put(),[
                'title'=>'require|max:30|token',
                'content'=>'require|max:1000'
            ]);
            if($err_msg !== true){
                return renderJs($err_msg);
            }

            //验证用户是否更改id值修改其他文章
            $post_info=BgPosts::where('status',1)->where('id',$id)->find();
            if(!$post_info || session('uid') !== $post_info->uid || !session('uid')){
                return renderJs('非法操作','/postss');
            }
            //写入数据库
            $post_info->title=$title;
            $post_info->content=$content;

            $post_info->save();

            //渲染
            return renderJs('修改文章成功',"/posts/{$id}/show");
            //return true;
        }

        return renderJs('非法操作','/postss');
    }

    public function delete($id){
        $post_info=BgPosts::where('status',1)->where('id',$id)->find();
        if(!$post_info || session('uid') !==$post_info->uid || !session('uid')){
            return renderJs('非法操作','/postss');
        }

        //更改状态 1=>0
        $post_info->status=0;
        $post_info->save();

        return renderJs('删除文章成功','/postss');
    }

    //评论
    public function comment(Request $request){
        if($request->isPost()){
            $post_id=$request->post('post_id');
            $content=$request->post('content');
            $post_info=BgPosts::get($post_id);
            if(!$post_info){
                return renderJs('非法操作');
            }

            //验证
            $err_msg=$this->validate($request->post(),[
                'post_id'=>'require|token',
                'content'=>'require|max:100'
            ],[
                'content.require'=>'评论不能为空',
                'content.max'=>'评论最大不超过100字符'
            ]);

            if($err_msg !==true){
                return renderJs($err_msg,"/posts/{$post_id}/show");
            }

            //写入数据库
            $comment=new BgComments;
            $comment->post_id=$post_id;
            $comment->user_id=session('uid');
            $comment->content=$content;
            $comment->save();

            return renderJs('发表评论成功',"/posts/{$post_id}/show");
        }
    }

    //回复评论
    public function reply(Request $request){
        if($request->isPost()){
            //dump($request->post());

            //获取参数
            $pid=$request->post('pid');
            $content=$request->post('content');
            $post_id=$request->post('post_id');

            //验证
            $err_msg=$this->validate($request->post(),[
                'pid'=>'require|token',
                'content'=>'require|max:100'
            ]);
            $comment_info=BgComments::where('id',$pid)->where('post_id',$post_id)->find();
            if(!$pid || !$comment_info){
                return renderJs('非法操作','/postss');
            }

            //TODO 不知道如何跳转解决问题
            if($err_msg !== true){
                return renderJs($err_msg,"/posts/{$post_id}/show");
            }


            //写入数据库
            $reply_comment=new BgComments;
            $reply_comment->pid=$pid;
            $reply_comment->post_id=$post_id;
            $reply_comment->user_id=session('uid');
            $reply_comment->content=$content;
            $reply_comment->save();

            return renderJs('回复成功',"/posts/{$post_id}/show");
        }
    }

    //处理图片上传
    public function image(Request $request){
        dump($request->get());
    }

    //点赞模块
    public function zan(Request $request,$id){
        $post_info=BgPosts::get($id);
        if(!$post_info){
            return renderJs('非法操作');
        }

        if($post_info->uid == session('uid')){
            return renderJs('不能点赞自己的文章',"/posts/{$id}/show");
        }
        //点赞
        $zan=Zans::onlyTrashed()->where('post_id',$id)->where('user_id',session('uid'))->find();

        if(!$zan) {
            $zan = new Zans;
            $zan->user_id = session('uid');
            $zan->post_id = $id;
            $zan->save();
        }else{
            $zan->restore();
        }

        return $this->redirect("/posts/{$id}/show");
    }

    //取消赞
    public function unzan($id){
        $post_info=BgPosts::get($id);
        if(!$post_info){
            return renderJs('非法操作');
        }

        if($post_info->uid == session('uid')){
            return renderJs('不能点赞自己的文章',"/posts/{$id}/show");
        }

        //取消赞
        $zan_info=Zans::where('post_id',$id)->where('user_id',session('uid'))->find();
        if(!$zan_info){
            return renderJs('不存在此条赞信息',"/posts/{$id}/show");
        }

        $zan_info->delete();

        return $this->redirect("/posts/{$id}/show");
    }
}
